package kr.or.eclipse.cedt;

import static kr.or.eclipse.swt.query.SWTQuery.$;
import kr.or.eclipse.swt.query.IWidgetFunction;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.json.JSONException;
import org.json.JSONObject;

public class TestPage extends FormPage implements IManifestEditorPage, IEditorPart {
	private Text nameField;
	private Text versionField;
	private Text txtNewText_2;
	private Text txtNewText_3;
	private Text txtNewText_4;
	private Text txtNewText_5;
	private Text txtNewText_6;
	private Text txtNewText_7;
	private Text txtNewText_8;
	private Text txtNewText_9;
	private Text backgroundPageField;
	private Table table;
	private Text optionPageField;
	private Text txtNewText_12;
	private Text txtNewText_13;

	/**
	 * Create the form page.
	 * 
	 * @param id
	 * @param title
	 */
	public TestPage(String id, String title) {
		super(id, title);
	}

	/**
	 * Create the form page.
	 * 
	 * @param editor
	 * @param id
	 * @param title
	 * @wbp.parser.constructor
	 * @wbp.eval.method.parameter id "Some id"
	 * @wbp.eval.method.parameter title "Some title"
	 */
	public TestPage(FormEditor editor, String id, String title) {
		super(editor, id, title);
		setPartName("Basic");
	}

	/**
	 * Create contents of the form.
	 * 
	 * @param managedForm
	 */
	@Override
	protected void createFormContent(IManagedForm managedForm) {
		FormToolkit toolkit = managedForm.getToolkit();
		ScrolledForm form = managedForm.getForm();
		form.setText("Chrome Extension Maifest Editor");
		Composite body = form.getBody();
		toolkit.decorateFormHeading(form.getForm());
		toolkit.paintBordersFor(body);
		FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
		fillLayout.spacing = 10;
		fillLayout.marginWidth = 10;
		fillLayout.marginHeight = 10;
		managedForm.getForm().getBody().setLayout(fillLayout);

		Composite composite_2 = managedForm.getToolkit().createComposite(managedForm.getForm().getBody(), SWT.NONE);
		managedForm.getToolkit().paintBordersFor(composite_2);
		composite_2.setLayout(new GridLayout(1, false));

		Section sctnNewSection = managedForm.getToolkit().createSection(composite_2, Section.TWISTIE | Section.TITLE_BAR);
		sctnNewSection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
		sctnNewSection.setSize(515, 152);
		managedForm.getToolkit().paintBordersFor(sctnNewSection);
		sctnNewSection.setText("Basic");
		sctnNewSection.setExpanded(true);

		Composite composite = managedForm.getToolkit().createComposite(sctnNewSection, SWT.NONE);
		managedForm.getToolkit().paintBordersFor(composite);
		sctnNewSection.setClient(composite);
		composite.setLayout(new GridLayout(3, false));

		Label lblNewLabel = managedForm.getToolkit().createLabel(composite, "&Name", SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.nameField = managedForm.getToolkit().createText(composite, "New Text", SWT.NONE);
		this.nameField.setText("");
		this.nameField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Label lblNewLabel_1 = managedForm.getToolkit().createLabel(composite, "&Version", SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.versionField = managedForm.getToolkit().createText(composite, "New Text", SWT.NONE);
		this.versionField.setText("");
		this.versionField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Label lblNewLabel_5 = managedForm.getToolkit().createLabel(composite, "&Description", SWT.NONE);
		lblNewLabel_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));

		this.txtNewText_5 = managedForm.getToolkit().createText(composite, "New Text", SWT.V_SCROLL | SWT.MULTI);
		GridData gd_txtNewText_5 = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
		gd_txtNewText_5.heightHint = 60;
		this.txtNewText_5.setLayoutData(gd_txtNewText_5);

		Hyperlink hprlnkNewHyperlink_2 = managedForm.getToolkit().createHyperlink(composite, "Background Page", SWT.NONE);
		hprlnkNewHyperlink_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		managedForm.getToolkit().paintBordersFor(hprlnkNewHyperlink_2);

		this.backgroundPageField = managedForm.getToolkit().createText(composite, "New Text", SWT.NONE);
		this.backgroundPageField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton_7 = managedForm.getToolkit().createButton(composite, "Browse...", SWT.NONE);

		Hyperlink hprlnkNewHyperlink_3 = managedForm.getToolkit().createHyperlink(composite, "Option Page", SWT.NONE);
		hprlnkNewHyperlink_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		managedForm.getToolkit().paintBordersFor(hprlnkNewHyperlink_3);

		this.optionPageField = managedForm.getToolkit().createText(composite, "New Text", SWT.NONE);
		this.optionPageField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton_12 = managedForm.getToolkit().createButton(composite, "Browse...", SWT.NONE);

		Section sctnNewSection_2 = managedForm.getToolkit().createSection(composite_2, Section.TWISTIE | Section.TITLE_BAR);
		sctnNewSection_2.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
		managedForm.getToolkit().paintBordersFor(sctnNewSection_2);
		sctnNewSection_2.setText("Browser Action");
		sctnNewSection_2.setExpanded(true);

		Composite composite_4 = managedForm.getToolkit().createComposite(sctnNewSection_2, SWT.NONE);
		managedForm.getToolkit().paintBordersFor(composite_4);
		sctnNewSection_2.setClient(composite_4);
		composite_4.setLayout(new GridLayout(3, false));

		Label lblNewLabel_6 = managedForm.getToolkit().createLabel(composite_4, "Default Icon", SWT.NONE);
		lblNewLabel_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.txtNewText_6 = managedForm.getToolkit().createText(composite_4, "New Text", SWT.NONE);
		this.txtNewText_6.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton_3 = managedForm.getToolkit().createButton(composite_4, "Browse...", SWT.NONE);

		Label lblNewLabel_8 = managedForm.getToolkit().createLabel(composite_4, "Default Title", SWT.NONE);
		lblNewLabel_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.txtNewText_12 = managedForm.getToolkit().createText(composite_4, "New Text", SWT.NONE);
		this.txtNewText_12.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Hyperlink hprlnkNewHyperlink = managedForm.getToolkit().createHyperlink(composite_4, "Default Popup", SWT.NONE);
		managedForm.getToolkit().paintBordersFor(hprlnkNewHyperlink);

		this.txtNewText_7 = managedForm.getToolkit().createText(composite_4, "New Text", SWT.NONE);
		this.txtNewText_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton_4 = managedForm.getToolkit().createButton(composite_4, "Browse...", SWT.NONE);

		Section sctnPageAction = managedForm.getToolkit().createSection(composite_2, Section.TWISTIE | Section.TITLE_BAR);
		sctnPageAction.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		managedForm.getToolkit().paintBordersFor(sctnPageAction);
		sctnPageAction.setText("Page Action");
		sctnPageAction.setExpanded(true);

		Composite composite_5 = managedForm.getToolkit().createComposite(sctnPageAction, SWT.NONE);
		managedForm.getToolkit().paintBordersFor(composite_5);
		sctnPageAction.setClient(composite_5);
		composite_5.setLayout(new GridLayout(3, false));

		Label lblNewLabel_7 = managedForm.getToolkit().createLabel(composite_5, "Default Icon", SWT.NONE);
		lblNewLabel_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.txtNewText_8 = managedForm.getToolkit().createText(composite_5, "New Text", SWT.NONE);
		this.txtNewText_8.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton_5 = managedForm.getToolkit().createButton(composite_5, "Browse...", SWT.NONE);

		Label lblNewLabel_9 = managedForm.getToolkit().createLabel(composite_5, "Default Title", SWT.NONE);
		lblNewLabel_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.txtNewText_13 = managedForm.getToolkit().createText(composite_5, "New Text", SWT.NONE);
		this.txtNewText_13.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Hyperlink hprlnkNewHyperlink_1 = managedForm.getToolkit().createHyperlink(composite_5, "Default Page", SWT.NONE);
		managedForm.getToolkit().paintBordersFor(hprlnkNewHyperlink_1);

		this.txtNewText_9 = managedForm.getToolkit().createText(composite_5, "New Text", SWT.NONE);
		this.txtNewText_9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton_6 = managedForm.getToolkit().createButton(composite_5, "Browse...", SWT.NONE);

		Composite composite_3 = managedForm.getToolkit().createComposite(managedForm.getForm().getBody(), SWT.NONE);
		managedForm.getToolkit().paintBordersFor(composite_3);
		composite_3.setLayout(new GridLayout(1, false));

		Section sctnNewSection_1 = managedForm.getToolkit().createSection(composite_3, Section.TWISTIE | Section.TITLE_BAR);
		sctnNewSection_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
		sctnNewSection_1.setSize(220, 119);
		managedForm.getToolkit().paintBordersFor(sctnNewSection_1);
		sctnNewSection_1.setText("Icon");
		sctnNewSection_1.setExpanded(true);

		Composite composite_1 = managedForm.getToolkit().createComposite(sctnNewSection_1, SWT.NONE);
		managedForm.getToolkit().paintBordersFor(composite_1);
		sctnNewSection_1.setClient(composite_1);
		composite_1.setLayout(new GridLayout(3, false));

		Label lblNewLabel_2 = managedForm.getToolkit().createLabel(composite_1, "16 x 16", SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.txtNewText_2 = managedForm.getToolkit().createText(composite_1, "New Text", SWT.NONE);
		this.txtNewText_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton = managedForm.getToolkit().createButton(composite_1, "Browse...", SWT.NONE);

		Label lblNewLabel_3 = managedForm.getToolkit().createLabel(composite_1, "48 x 48", SWT.NONE);
		lblNewLabel_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.txtNewText_3 = managedForm.getToolkit().createText(composite_1, "New Text", SWT.NONE);
		this.txtNewText_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton_1 = managedForm.getToolkit().createButton(composite_1, "Browse...", SWT.NONE);

		Label lblNewLabel_4 = managedForm.getToolkit().createLabel(composite_1, "128 x 128", SWT.NONE);
		lblNewLabel_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));

		this.txtNewText_4 = managedForm.getToolkit().createText(composite_1, "New Text", SWT.NONE);
		this.txtNewText_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton_2 = managedForm.getToolkit().createButton(composite_1, "Browse...", SWT.NONE);

		Section sctnNewSection_3 = managedForm.getToolkit().createSection(composite_3, Section.TWISTIE | Section.TITLE_BAR);
		sctnNewSection_3.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
		managedForm.getToolkit().paintBordersFor(sctnNewSection_3);
		sctnNewSection_3.setText("Permission");
		sctnNewSection_3.setExpanded(true);

		Composite composite_6 = managedForm.getToolkit().createComposite(sctnNewSection_3, SWT.NONE);
		managedForm.getToolkit().paintBordersFor(composite_6);
		sctnNewSection_3.setClient(composite_6);
		composite_6.setLayout(new GridLayout(2, false));

		TableViewer tableViewer = new TableViewer(composite_6, SWT.BORDER | SWT.FULL_SELECTION);
		this.table = tableViewer.getTable();
		this.table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		managedForm.getToolkit().paintBordersFor(this.table);

		Composite composite_7 = managedForm.getToolkit().createComposite(composite_6, SWT.NONE);
		composite_7.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		managedForm.getToolkit().paintBordersFor(composite_7);
		GridLayout gl_composite_7 = new GridLayout(1, false);
		gl_composite_7.marginHeight = 0;
		gl_composite_7.marginWidth = 0;
		composite_7.setLayout(gl_composite_7);

		Button btnNewButton_8 = managedForm.getToolkit().createButton(composite_7, "Add", SWT.NONE);
		btnNewButton_8.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

		Button btnNewButton_9 = managedForm.getToolkit().createButton(composite_7, "Remove", SWT.NONE);
		btnNewButton_9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

		Button btnNewButton_10 = managedForm.getToolkit().createButton(composite_7, "Up", SWT.NONE);
		btnNewButton_10.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

		Button btnNewButton_11 = managedForm.getToolkit().createButton(composite_7, "Down", SWT.NONE);
		btnNewButton_11.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

		Section sctnNewSection_4 = managedForm.getToolkit().createSection(composite_3, Section.TWISTIE | Section.TITLE_BAR);
		sctnNewSection_4.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
		managedForm.getToolkit().paintBordersFor(sctnNewSection_4);
		sctnNewSection_4.setText("Overview");
		sctnNewSection_4.setExpanded(true);

		Composite composite_8 = managedForm.getToolkit().createComposite(sctnNewSection_4, SWT.NONE);
		managedForm.getToolkit().paintBordersFor(composite_8);
		sctnNewSection_4.setClient(composite_8);
		composite_8.setLayout(new GridLayout(1, false));

		Canvas canvas = new Canvas(composite_8, SWT.BORDER);
		canvas.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		managedForm.getToolkit().adapt(canvas);
		managedForm.getToolkit().paintBordersFor(canvas);

		ImageHyperlink mghprlnkNewImagehyperlink = managedForm.getToolkit().createImageHyperlink(composite_8, SWT.NONE);
		managedForm.getToolkit().paintBordersFor(mghprlnkNewImagehyperlink);
		mghprlnkNewImagehyperlink.setText("Deploy to Chrome in development mode");

		ImageHyperlink mghprlnkNewImagehyperlink_1 = managedForm.getToolkit().createImageHyperlink(composite_8, SWT.NONE);
		managedForm.getToolkit().paintBordersFor(mghprlnkNewImagehyperlink_1);
		mghprlnkNewImagehyperlink_1.setText("Visit Developer Dashboard Page");

		hook(body);
	}

	private void hook(Composite body) {
		$(body, "button[text='Browse...']").addListener(SWT.Selection, (new Listener() {
			@Override
			public void handleEvent(Event event) {
				FileDialog dialog = new FileDialog(Display.getDefault().getActiveShell());
				String result = dialog.open();
				if (result != null) {
					$(event.widget).prev().setText(result);
				}
			}
		}));

		body.layout(true, true);
	}

	@Override
	public void updateViewWithModel(JSONObject model) {
		try {
			String name = model.getString("name");
			System.out.println(name);
			nameField.setText(name);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updateModel(JSONObject model) {

	}
}
